﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Web;
using WebHttpApi.WebHttpApi.Core;

namespace WebHttpApi.WebHttpApi.HttpApi
{
    /// <summary>
    /// TyJzq 的摘要说明：安美通集中器指令类
    /// </summary>
    public class TyJzq
    {
        public static string str_DeviceId = "";                //集中器地址逆序排列
        public static string str_MonitorPoint = "";            //表监测点号逆序排列
        public static string str_MeterType = "";               //表类型 16进制

        public static string str_MeterId = "";                 //表地址逆序排列
        public static string str_Comm = "";                    //命令串
        public static string str_CommPam = "";                 //参数串

        public static string str_zxjg = "0";                   //执行结果  -  0：未执行 | 1：已执行
        public static string str_xyjx = "";                    //协议解析  -  什么命令（日志）
        public static string str_rzlx = "发送";                //日志类型  -  存储：命令写数据库（日志）
        public static string str_xymc = "";                    //协议名称  -  命令类型名称： 控阀、休眠等（日志）
        public static string str_oper = "WebHttpApi";          //操作员    -  指令、日志

        public static string str_Result = "";
        public static string type_100_DeviceId = "";
        public static string jcdbh_q = "";
        public static string jcdbh_z = "";

        public static string Jzq_Comm(string DeviceType, string DeviceId, string Type, string CommPam)
        {
            str_xyjx = "";
            str_xymc = "";

            if (DeviceId != "" && Type != "")
            {
                try
                {
                    str_DeviceId = DeviceId.Substring(8, 2) +
                                   DeviceId.Substring(6, 2) +
                                   DeviceId.Substring(4, 2) +
                                   DeviceId.Substring(2, 2) +
                                   DeviceId.Substring(0, 2);

                    switch (DeviceType)
                    {
                        //安美通集中器
                        case "0":
                            {
                                switch (Type)
                                {
                                    //设置集中器时间
                                    //Type：1 | 01
                                    case "1":
                                        {
                                            str_xymc = "设置集中器时间";
                                            str_xyjx = "设置集中器时间";

                                            string str_year = DateTime.Now.ToString("yyyy");
                                            str_year = "0000" + str_year;
                                            str_year = str_year.Substring(str_year.Length - 4);
                                            str_year = str_year.Substring(str_year.Length - 2);

                                            string str_month = DateTime.Now.ToString("MM");
                                            str_month = "00" + str_month;
                                            str_month = str_month.Substring(str_month.Length - 2);

                                            string str_day = DateTime.Now.ToString("dd");
                                            str_day = "00" + str_day;
                                            str_day = str_day.Substring(str_day.Length - 2);

                                            string str_hour = DateTime.Now.ToString("HH");
                                            str_hour = "00" + str_hour;
                                            str_hour = str_hour.Substring(str_hour.Length - 2);

                                            string str_minute = DateTime.Now.ToString("mm");
                                            str_minute = "00" + str_minute;
                                            str_minute = str_minute.Substring(str_minute.Length - 2);

                                            string str_second = DateTime.Now.ToString("ss");
                                            str_second = "00" + str_second;
                                            str_second = str_second.Substring(str_second.Length - 2);

                                            string str_weekday = DateTime.Now.DayOfWeek.ToString();
                                            switch (str_weekday)
                                            {
                                                case "Monday":
                                                    {
                                                        str_weekday = "01";
                                                        break;
                                                    }

                                                case "Tuesday":
                                                    {
                                                        str_weekday = "02";
                                                        break;
                                                    }

                                                case "Wednesday":
                                                    {
                                                        str_weekday = "03";
                                                        break;
                                                    }

                                                case "Thursday":
                                                    {
                                                        str_weekday = "04";
                                                        break;
                                                    }

                                                case "Friday":
                                                    {
                                                        str_weekday = "05";
                                                        break;
                                                    }

                                                case "Saturday":
                                                    {
                                                        str_weekday = "06";
                                                        break;
                                                    }

                                                case "Sunday":
                                                    {
                                                        str_weekday = "00";
                                                        break;
                                                    }
                                            }

                                            str_Comm = str_second + str_minute + str_hour + str_day + str_month + str_year + str_weekday;

                                            str_Comm = "68EE" + str_DeviceId + "010800" + str_Comm + "01";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //读取集中器状态 
                                    //Type：2 | 02
                                    case "2":
                                        {
                                            str_xymc = "读取集中器状态";
                                            str_xyjx = "读取集中器状态";

                                            str_Comm = "68EE" + str_DeviceId + "02010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //查询集中器软件版本号 
                                    //Type：3 | 03
                                    case "3":
                                        {
                                            str_xymc = "查询集中器软件版本号";
                                            str_xyjx = "查询集中器软件版本号";

                                            str_Comm = "68EE" + str_DeviceId + "03010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //设置集中器地址 
                                    //Type：4 | 04
                                    //CommPam：⑴集中器地址
                                    case "4":
                                        {
                                            str_xymc = "设置集中器地址";
                                            str_xyjx = "设置集中器地址";

                                            CommPam = "0000000000" + CommPam;
                                            CommPam = CommPam.Substring(CommPam.Length - 10);

                                            string str_x_DeviceId = CommPam.Substring(8, 2) +
                                                                    CommPam.Substring(6, 2) +
                                                                    CommPam.Substring(4, 2) +
                                                                    CommPam.Substring(2, 2) +
                                                                    CommPam.Substring(0, 2);

                                            str_Comm = "68EE" + str_DeviceId + "040600" + str_x_DeviceId + "01";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //读取集中器时间 
                                    //Type：5 | 05
                                    case "5":
                                        {
                                            str_xymc = "读取集中器时间";
                                            str_xyjx = "读取集中器时间";

                                            str_Comm = "68EE" + str_DeviceId + "05010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //设置集中器抄表参数 
                                    //Type：6 | 06
                                    //CommPam：⑴抄表间隔（分钟）：⑵日冻结时刻（0-23）：⑶月冻结时刻（0-23）：⑷月冻结时间（1-31日）：⑸冻结开关
                                    case "6":
                                        {
                                            str_xymc = "设置集中器抄表参数";
                                            str_xyjx = "设置集中器抄表参数";

                                            try
                                            {
                                                string[] str = CommPam.Split(':');

                                                //抄表间隔
                                                string str_cbjg = str[0];
                                                str_cbjg = Convert.ToString(Convert.ToInt16(str_cbjg), 16);
                                                str_cbjg = "0000" + str_cbjg;
                                                str_cbjg = str_cbjg.Substring(str_cbjg.Length - 4);
                                                str_cbjg = str_cbjg.Substring(2, 2) +
                                                           str_cbjg.Substring(0, 2);

                                                //日冻结时刻
                                                string str_rdjsk = str[1];
                                                str_rdjsk = Convert.ToString(Convert.ToInt16(str_rdjsk), 16);
                                                str_rdjsk = "00" + str_rdjsk;
                                                str_rdjsk = str_rdjsk.Substring(str_rdjsk.Length - 2);

                                                //月冻结时刻
                                                string str_ydjsk = str[2];
                                                str_ydjsk = Convert.ToString(Convert.ToInt16(str_ydjsk), 16);
                                                str_ydjsk = "00" + str_ydjsk;
                                                str_ydjsk = str_ydjsk.Substring(str_ydjsk.Length - 2);

                                                //月冻结时间
                                                string str_ydjsj = str[3];
                                                str_ydjsj = Convert.ToString(Convert.ToInt16(str_ydjsj), 16);
                                                str_ydjsj = "00" + str_ydjsj;
                                                str_ydjsj = str_ydjsj.Substring(str_ydjsj.Length - 2);

                                                //冻结开关
                                                string str_djkg = str[4];
                                                str_djkg = "00" + str_djkg;
                                                str_djkg = str_djkg.Substring(str_djkg.Length - 2);

                                                str_Comm = "68EE" + str_DeviceId + "060700" + str_cbjg + str_rdjsk + str_ydjsk + str_ydjsj + str_djkg + "01";
                                                str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                                str_Comm = str_Comm + "16";
                                                str_Comm = str_Comm.ToUpper();

                                                str_Result = Jzq_sql(DeviceId, Type);
                                            }
                                            catch
                                            {
                                                str_Result = "参数错误";
                                                break;
                                            }

                                            break;
                                        }

                                    //读取集中器抄表参数 
                                    //Type：7 | 07
                                    case "7":
                                        {
                                            str_xymc = "读取集中器抄表参数";
                                            str_xyjx = "读取集中器抄表参数";

                                            str_Comm = "68EE" + str_DeviceId + "07010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //设置集中器GPRS参数 
                                    //Type：8 | 08
                                    //CommPam：⑴GPRS连接方式：⑵IP：⑶端口：⑷APN：⑸DNS：⑹心跳间隔
                                    case "8":
                                        {
                                            str_xymc = "设置集中器GPRS参数";
                                            str_xyjx = "设置集中器GPRS参数";

                                            try
                                            {
                                                string[] str = CommPam.Split(':');

                                                //GPRS连接方式
                                                string str_ljfs = str[0];
                                                str_ljfs = "00" + str_ljfs;
                                                str_ljfs = str_ljfs.Substring(str_ljfs.Length - 2);

                                                //IP
                                                string str_ip = str[1];
                                                string[] ip = str_ip.Split('.');

                                                string str_ip01 = "00" + Convert.ToInt64(ip[0]).ToString("x");
                                                str_ip01 = str_ip01.Substring(str_ip01.Length - 2);
                                                string str_ip02 = "00" + Convert.ToInt64(ip[1]).ToString("x");
                                                str_ip02 = str_ip02.Substring(str_ip02.Length - 2);
                                                string str_ip03 = "00" + Convert.ToInt64(ip[2]).ToString("x");
                                                str_ip03 = str_ip03.Substring(str_ip03.Length - 2);
                                                string str_ip04 = "00" + Convert.ToInt64(ip[3]).ToString("x");
                                                str_ip04 = str_ip04.Substring(str_ip04.Length - 2);

                                                str_ip = str_ip01 + str_ip02 + str_ip03 + str_ip04;

                                                //端口
                                                string str_dk = str[2];

                                                str_dk = Convert.ToInt64(str_dk).ToString("x");
                                                str_dk = "0000" + str_dk;
                                                str_dk = str_dk.Substring(str_dk.Length - 4);
                                                str_dk = str_dk.Substring(2, 2) +
                                                         str_dk.Substring(0, 2);

                                                //APN
                                                string str_apn = str[3];

                                                byte[] array_apn = System.Text.Encoding.ASCII.GetBytes(str_apn);

                                                string asc_apn = null;
                                                for (int i = 0; i < array_apn.Length; i++)
                                                {
                                                    int asc_code = (int)(array_apn[i]);
                                                    asc_apn += Convert.ToString(asc_code, 16);
                                                }
                                                str_apn = asc_apn + "00000000000000000000000000000000";
                                                str_apn=str_apn.Substring(0,32);

                                                //DNS
                                                string str_dns = str[4];

                                                byte[] array_dns = System.Text.Encoding.ASCII.GetBytes(str_dns);

                                                string asc_dns = null;
                                                for (int i = 0; i < array_dns.Length; i++)
                                                {
                                                    int asc_code = (int)(array_dns[i]);
                                                    asc_dns += Convert.ToString(asc_code, 16);
                                                }
                                                str_dns = asc_dns + "00000000000000000000000000000000000000000000000000000000000000000000000000";
                                                str_dns = str_dns.Substring(0, 74);

                                                //心跳间隔
                                                string str_heart = str[5];
                                                str_heart = Convert.ToString(Convert.ToInt16(str_heart), 16);
                                                str_heart = "00" + str_heart;
                                                str_heart = str_heart.Substring(str_heart.Length - 2);

                                                str_Comm = "68EE" + str_DeviceId + "083E00" + str_ljfs + str_ip + str_dk + str_apn + str_dns + str_heart + "01";
                                                str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                                str_Comm = str_Comm + "16";
                                                str_Comm = str_Comm.ToUpper();

                                                str_Result = Jzq_sql(DeviceId, Type);
                                            }
                                            catch
                                            {
                                                str_Result = "参数错误";
                                                break;
                                            }

                                            break;
                                        }

                                    //读取集中器GPRS参数 
                                    //Type：9 | 09
                                    case "9":
                                        {
                                            str_xymc = "读取集中器GPRS参数";
                                            str_xyjx = "读取集中器GPRS参数";

                                            str_Comm = "68EE" + str_DeviceId + "09010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //下载表计地址
                                    //Type：10 | 10
                                    //CommPam：⑴监测点号.表类型.表地址,监测点号.表类型.表地址：⑵规约：⑶通道号：⑷通讯参数：⑸采集器地址：⑹大小类号
                                    case "10":
                                        {
                                            str_xymc = "下载表计地址";
                                            str_xyjx = "下载表计地址";

                                            try
                                            {
                                                string str_data = "";

                                                string[] str = CommPam.Split(':');

                                                //监测点号、表类型、表地址 数组
                                                string arr_str_jcdh_blx_ybdz = str[0];
                                                //监测点号、表类型、表地址 
                                                string[] str_jcdh_blx_ybdz = arr_str_jcdh_blx_ybdz.Split(',');

                                                //仪表数量小于等于20
                                                if (str_jcdh_blx_ybdz.Length <= 20)
                                                {
                                                    for (int i = 0; i < str_jcdh_blx_ybdz.Length; i++)
                                                    {
                                                        string jcdh_blx_ybdz = str_jcdh_blx_ybdz[i];
                                                        string[] str01 = jcdh_blx_ybdz.Split('.');

                                                        //监测点号
                                                        string str_jcdh = str01[0];
                                                        str_jcdh = Convert.ToString(Convert.ToInt16(str_jcdh), 16);
                                                        str_jcdh = "0000" + str_jcdh;
                                                        str_jcdh = str_jcdh.Substring(str_jcdh.Length - 4);
                                                        str_jcdh = str_jcdh.Substring(2, 2) +
                                                                   str_jcdh.Substring(0, 2);

                                                        //表类型
                                                        string str_blx = str01[1];
                                                        str_blx = Convert.ToString(Convert.ToInt16(str_blx), 16);
                                                        str_blx = "00" + str_blx;
                                                        str_blx = str_blx.Substring(str_blx.Length - 2);

                                                        //表地址
                                                        string str_ybdz = str01[2];
                                                        str_ybdz = "00000000000000" + str_ybdz;
                                                        str_ybdz = str_ybdz.Substring(str_ybdz.Length - 14);
                                                        str_ybdz = str_ybdz.Substring(12, 2) +
                                                                   str_ybdz.Substring(10, 2) +
                                                                   str_ybdz.Substring(8, 2) +
                                                                   str_ybdz.Substring(6, 2) +
                                                                   str_ybdz.Substring(4, 2) +
                                                                   str_ybdz.Substring(2, 2) +
                                                                   str_ybdz.Substring(0, 2);

                                                        //规约
                                                        string str_gy = str[1];
                                                        str_gy = "00" + str_gy;
                                                        str_gy = str_gy.Substring(str_gy.Length - 2);

                                                        //通道号
                                                        string str_tdh = str[2];
                                                        str_tdh = Convert.ToString(Convert.ToInt16(str_tdh), 16);
                                                        str_tdh = "00" + str_tdh;
                                                        str_tdh = str_tdh.Substring(str_tdh.Length - 2);

                                                        //通讯参数
                                                        string str_txcs = str[3];
                                                        str_txcs = "00" + str_txcs;
                                                        str_txcs = str_txcs.Substring(str_txcs.Length - 2);

                                                        //采集器号
                                                        string str_cjqdz = str[4];
                                                        str_cjqdz = "0000000000" + str_cjqdz;
                                                        str_cjqdz = str_cjqdz.Substring(str_cjqdz.Length - 10);
                                                        str_cjqdz = str_cjqdz.Substring(8, 2) +
                                                                    str_cjqdz.Substring(6, 2) +
                                                                    str_cjqdz.Substring(4, 2) +
                                                                    str_cjqdz.Substring(2, 2) +
                                                                    str_cjqdz.Substring(0, 2);

                                                        //大小类号
                                                        string str_dxlh = str[5];
                                                        str_dxlh = "00" + str_dxlh;
                                                        str_dxlh = str_dxlh.Substring(str_dxlh.Length - 2);

                                                        str_data += str_jcdh + str_blx + str_ybdz + str_gy + str_tdh + str_txcs + str_cjqdz + str_dxlh;
                                                    }

                                                    //数据区长度
                                                    string str_data_len = Convert.ToString(19 * str_jcdh_blx_ybdz.Length + 2, 16);
                                                    str_data_len = "0000" + str_data_len;
                                                    str_data_len = str_data_len.Substring(str_data_len.Length - 4);
                                                    str_data_len = str_data_len.Substring(2, 2) +
                                                                   str_data_len.Substring(0, 2);

                                                    //下载仪表数量
                                                    string str_meter_count = Convert.ToString(str_jcdh_blx_ybdz.Length, 16);
                                                    str_meter_count = "00" + str_meter_count;
                                                    str_meter_count = str_meter_count.Substring(str_meter_count.Length - 2);

                                                    str_Comm = "68EE" + str_DeviceId + "10" + str_data_len + str_meter_count + str_data + "01";
                                                    str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                                    str_Comm = str_Comm + "16";
                                                    str_Comm = str_Comm.ToUpper();

                                                    str_Result = Jzq_sql(DeviceId, Type);
                                                }
                                                else
                                                {
                                                    str_Result = "大于20只仪表";
                                                    break;
                                                }
                                            }
                                            catch
                                            {
                                                str_Result = "参数错误";
                                                break;
                                            }
                                            break;
                                        }

                                    //自动下载表计档案
                                    //Type：100 | 100
                                    case "100":
                                        {
                                            //2020-12-16
                                            //自动下载档案:多线程处理
                                            type_100_DeviceId = DeviceId;

                                            Thread threadA = new Thread(jzq_type_100);
                                            threadA.Start();  

                                            break;
                                        }

                                    //自动删除下载表计档案
                                    //Type：101 | 101
                                    case "101":
                                        {
                                            //2020-12-16
                                            //自动下载档案:多线程处理
                                            type_100_DeviceId = DeviceId;

                                            Thread threadA = new Thread(jzq_type_101);
                                            threadA.Start();

                                            break;
                                        }

                                    //输入起、止监测点编号删除集中器中表计档案
                                    //自动删除下载表计档案
                                    //Type：102 | 102
                                    case "102":
                                        {
                                            //2020-12-16
                                            //自动下载档案:多线程处理
                                            type_100_DeviceId = DeviceId;

                                            string[] str = CommPam.Split(':');

                                            //监测点号、表类型、表地址 数组
                                            jcdbh_q = str[0];
                                            jcdbh_z = str[1];

                                            Thread threadA = new Thread(jzq_type_102);
                                            threadA.Start();

                                            break;
                                        }

                                    //读取下载表计信息
                                    //Type：11 | 11
                                    //CommPam：⑴监测点号,监测点号
                                    case "11":
                                        {
                                            str_xymc = "读取下载表计信息";
                                            str_xyjx = "读取下载表计信息";

                                            try
                                            {
                                                string str_data = "";

                                                string[] str = CommPam.Split(',');

                                                if (str.Length <= 20)
                                                {
                                                    for (int i = 0; i < str.Length; i++)
                                                    {
                                                        //监测点号
                                                        string str_jcdh = str[i];
                                                        str_jcdh = Convert.ToString(Convert.ToInt16(str_jcdh), 16);
                                                        str_jcdh = "0000" + str_jcdh;
                                                        str_jcdh = str_jcdh.Substring(str_jcdh.Length - 4);
                                                        str_jcdh = str_jcdh.Substring(2, 2) +
                                                                   str_jcdh.Substring(0, 2);

                                                        str_data += str_jcdh;
                                                    }

                                                    //数据区长度
                                                    string str_data_len = Convert.ToString(2 * str.Length + 2, 16);
                                                    str_data_len = "0000" + str_data_len;
                                                    str_data_len = str_data_len.Substring(str_data_len.Length - 4);
                                                    str_data_len = str_data_len.Substring(2, 2) +
                                                                   str_data_len.Substring(0, 2);

                                                    //下载仪表数量
                                                    string str_meter_count = Convert.ToString(str.Length, 16);
                                                    str_meter_count = "00" + str_meter_count;
                                                    str_meter_count = str_meter_count.Substring(str_meter_count.Length - 2);

                                                    str_Comm = "68EE" + str_DeviceId + "11" + str_data_len + str_meter_count + str_data + "01";
                                                    str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                                    str_Comm = str_Comm + "16";
                                                    str_Comm = str_Comm.ToUpper();

                                                    str_Result = Jzq_sql(DeviceId, Type);
                                                }
                                                else
                                                {
                                                    str_Result = "大于20只仪表";
                                                    break;
                                                }
                                            }
                                            catch
                                            {
                                                str_Result = "参数错误";
                                                break;
                                            }

                                            break;
                                        }

                                    //读取下载表计地址数量
                                    //Type：12 | 12
                                    case "12":
                                        {
                                            str_xymc = "读取下载表计地址数量";
                                            str_xyjx = "读取下载表计地址数量";

                                            str_Comm = "68EE" + str_DeviceId + "12010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //启动立即抄表
                                    //Type：15 | 15
                                    case "15":
                                        {
                                            str_xymc = "启动立即抄表";
                                            str_xyjx = "启动立即抄表";

                                            str_Comm = "68EE" + str_DeviceId + "15010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //读取表计当前数据
                                    //Type：16 | 16
                                    //CommPam：⑴监测点号,监测点号
                                    case "16":
                                        {
                                            str_xymc = "读取表计当前数据";
                                            str_xyjx = "读取表计当前数据";

                                            try
                                            {
                                                string str_data = "";

                                                string[] str = CommPam.Split(',');

                                                if (str.Length <= 20)
                                                {
                                                    for (int i = 0; i < str.Length; i++)
                                                    {
                                                        //监测点号
                                                        string str_jcdh = str[i];
                                                        str_jcdh = Convert.ToString(Convert.ToInt16(str_jcdh), 16);
                                                        str_jcdh = "0000" + str_jcdh;
                                                        str_jcdh = str_jcdh.Substring(str_jcdh.Length - 4);
                                                        str_jcdh = str_jcdh.Substring(2, 2) +
                                                                   str_jcdh.Substring(0, 2);

                                                        str_data += str_jcdh;
                                                    }

                                                    //数据区长度
                                                    string str_data_len = Convert.ToString(2 * str.Length + 2, 16);
                                                    str_data_len = "0000" + str_data_len;
                                                    str_data_len = str_data_len.Substring(str_data_len.Length - 4);
                                                    str_data_len = str_data_len.Substring(2, 2) +
                                                                   str_data_len.Substring(0, 2);

                                                    //下载仪表数量
                                                    string str_meter_count = Convert.ToString(str.Length, 16);
                                                    str_meter_count = "00" + str_meter_count;
                                                    str_meter_count = str_meter_count.Substring(str_meter_count.Length - 2);

                                                    str_Comm = "68EE" + str_DeviceId + "16" + str_data_len + str_meter_count + str_data + "01";
                                                    str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                                    str_Comm = str_Comm + "16";
                                                    str_Comm = str_Comm.ToUpper();

                                                    str_Result = Jzq_sql(DeviceId, Type);
                                                }
                                                else
                                                {
                                                    str_Result = "大于20只仪表";
                                                    break;
                                                }
                                            }
                                            catch
                                            {
                                                str_Result = "参数错误";
                                                break;
                                            }

                                            break;
                                        }

                                    //读取表计日冻结数据
                                    //Type：17 | 17
                                    //CommPam：⑴监测点号,监测点号:⑵年月日
                                    case "17":
                                        {
                                            str_xymc = "读取表计日冻结数据";
                                            str_xyjx = "读取表计日冻结数据";

                                            try
                                            {
                                                string str_data = "";

                                                string[] str = CommPam.Split(':');

                                                //监测点号 数组
                                                string arr_str_jcdh = str[0];
                                                string[] str01 = arr_str_jcdh.Split(',');

                                                if (str01.Length <= 20)
                                                {
                                                    for (int i = 0; i < str01.Length; i++)
                                                    {
                                                        //监测点号
                                                        string str_jcdh = str01[i];
                                                        str_jcdh = Convert.ToString(Convert.ToInt16(str_jcdh), 16);
                                                        str_jcdh = "0000" + str_jcdh;
                                                        str_jcdh = str_jcdh.Substring(str_jcdh.Length - 4);
                                                        str_jcdh = str_jcdh.Substring(2, 2) +
                                                                   str_jcdh.Substring(0, 2);

                                                        str_data += str_jcdh;
                                                    }

                                                    //年月日
                                                    string str_date = str[1];
                                                    str_date = "000000" + str_date;
                                                    str_date = str_date.Substring(str_date.Length - 6);
                                                    str_date = str_date.Substring(4, 2) +
                                                               str_date.Substring(2, 2) +
                                                               str_date.Substring(0, 2);

                                                    //数据区长度
                                                    string str_data_len = Convert.ToString(2 * str01.Length + 5, 16);
                                                    str_data_len = "0000" + str_data_len;
                                                    str_data_len = str_data_len.Substring(str_data_len.Length - 4);
                                                    str_data_len = str_data_len.Substring(2, 2) +
                                                                   str_data_len.Substring(0, 2);

                                                    //下载仪表数量
                                                    string str_meter_count = Convert.ToString(str01.Length, 16);
                                                    str_meter_count = "00" + str_meter_count;
                                                    str_meter_count = str_meter_count.Substring(str_meter_count.Length - 2);


                                                    str_Comm = "68EE" + str_DeviceId + "17" + str_data_len + str_date + str_meter_count + str_data + "01";
                                                    str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                                    str_Comm = str_Comm + "16";
                                                    str_Comm = str_Comm.ToUpper();

                                                    str_Result = Jzq_sql(DeviceId, Type);
                                                }
                                                else
                                                {
                                                    str_Result = "大于20只仪表";
                                                    break;
                                                }
                                            }
                                            catch
                                            {
                                                str_Result = "参数错误";
                                                break;
                                            }

                                            break;
                                        }

                                    //读单表的当前数据
                                    //Type：18 | 18
                                    //CommPam：⑴监测点号
                                    case "18":
                                        {
                                            str_xymc = "读单表的当前数据";
                                            str_xyjx = "读单表的当前数据";

                                            try
                                            {
                                                //监测点号
                                                string str_jcdh = CommPam;
                                                str_jcdh = Convert.ToString(Convert.ToInt16(str_jcdh), 16);
                                                str_jcdh = "0000" + str_jcdh;
                                                str_jcdh = str_jcdh.Substring(str_jcdh.Length - 4);
                                                str_jcdh = str_jcdh.Substring(2, 2) +
                                                           str_jcdh.Substring(0, 2);

                                                str_Comm = "68EE" + str_DeviceId + "18040001" + str_jcdh + "01";
                                                str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                                str_Comm = str_Comm + "16";
                                                str_Comm = str_Comm.ToUpper();

                                                str_Result = Jzq_sql(DeviceId, Type);
                                            }
                                            catch
                                            {
                                                str_Result = "参数错误";
                                                break;
                                            }

                                            break;
                                        }

                                    //读取表计月冻结数据
                                    //Type：20 | 20
                                    //CommPam：⑴监测点号,监测点号:⑵年月
                                    case "20":
                                        {
                                            str_xymc = "读取表计月冻结数据";
                                            str_xyjx = "读取表计月冻结数据";

                                            try
                                            {
                                                string str_data = "";

                                                string[] str = CommPam.Split(':');

                                                //监测点号 数组
                                                string arr_str_jcdh = str[0];
                                                string[] str01 = arr_str_jcdh.Split(',');

                                                if (str01.Length <= 20)
                                                {
                                                    for (int i = 0; i < str01.Length; i++)
                                                    {
                                                        //监测点号
                                                        string str_jcdh = str01[i];
                                                        str_jcdh = Convert.ToString(Convert.ToInt16(str_jcdh), 16);
                                                        str_jcdh = "0000" + str_jcdh;
                                                        str_jcdh = str_jcdh.Substring(str_jcdh.Length - 4);
                                                        str_jcdh = str_jcdh.Substring(2, 2) +
                                                                   str_jcdh.Substring(0, 2);

                                                        str_data += str_jcdh;
                                                    }

                                                    //年月
                                                    string str_date = str[1];
                                                    str_date = "0000" + str_date;
                                                    str_date = str_date.Substring(str_date.Length - 4);
                                                    str_date = str_date.Substring(2, 2) +
                                                               str_date.Substring(0, 2);

                                                    //数据区长度
                                                    string str_data_len = Convert.ToString(2 * str01.Length + 4, 16);
                                                    str_data_len = "0000" + str_data_len;
                                                    str_data_len = str_data_len.Substring(str_data_len.Length - 4);
                                                    str_data_len = str_data_len.Substring(2, 2) +
                                                                   str_data_len.Substring(0, 2);

                                                    //下载仪表数量
                                                    string str_meter_count = Convert.ToString(str01.Length, 16);
                                                    str_meter_count = "00" + str_meter_count;
                                                    str_meter_count = str_meter_count.Substring(str_meter_count.Length - 2);


                                                    str_Comm = "68EE" + str_DeviceId + "20" + str_data_len + str_date + str_meter_count + str_data + "01";
                                                    str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                                    str_Comm = str_Comm + "16";
                                                    str_Comm = str_Comm.ToUpper();

                                                    str_Result = Jzq_sql(DeviceId, Type);
                                                }
                                                else
                                                {
                                                    str_Result = "大于20只仪表";
                                                    break;
                                                }
                                            }
                                            catch
                                            {
                                                str_Result = "参数错误";
                                                break;
                                            }

                                            break;
                                        }

                                    //取控制记录
                                    //Type：25 | 25
                                    //CommPam：⑴监测点号,监测点号
                                    case "25":
                                        {
                                            str_xymc = "取控制记录";
                                            str_xyjx = "取控制记录";

                                            try
                                            {
                                                string str_data = "";

                                                string[] str = CommPam.Split(',');

                                                if (str.Length <= 20)
                                                {
                                                    for (int i = 0; i < str.Length; i++)
                                                    {
                                                        //监测点号
                                                        string str_jcdh = str[i];
                                                        str_jcdh = Convert.ToString(Convert.ToInt16(str_jcdh), 16);
                                                        str_jcdh = "0000" + str_jcdh;
                                                        str_jcdh = str_jcdh.Substring(str_jcdh.Length - 4);
                                                        str_jcdh = str_jcdh.Substring(2, 2) +
                                                                   str_jcdh.Substring(0, 2);

                                                        str_data += str_jcdh;
                                                    }

                                                    //数据区长度
                                                    string str_data_len = Convert.ToString(2 * str.Length + 2, 16);
                                                    str_data_len = "0000" + str_data_len;
                                                    str_data_len = str_data_len.Substring(str_data_len.Length - 4);
                                                    str_data_len = str_data_len.Substring(2, 2) +
                                                                   str_data_len.Substring(0, 2);

                                                    //下载仪表数量
                                                    string str_meter_count = Convert.ToString(str.Length, 16);
                                                    str_meter_count = "00" + str_meter_count;
                                                    str_meter_count = str_meter_count.Substring(str_meter_count.Length - 2);

                                                    str_Comm = "68EE" + str_DeviceId + "25" + str_data_len + str_meter_count + str_data + "01";
                                                    str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                                    str_Comm = str_Comm + "16";
                                                    str_Comm = str_Comm.ToUpper();

                                                    str_Result = Jzq_sql(DeviceId, Type);
                                                }
                                                else
                                                {
                                                    str_Result = "大于20只仪表";
                                                    break;
                                                }
                                            }
                                            catch
                                            {
                                                str_Result = "参数错误";
                                                break;
                                            }

                                            break;
                                        }

                                    //读取集中器地址
                                    //Type：50 | 50
                                    case "50":
                                        {
                                            str_xymc = "读取集中器地址";
                                            str_xyjx = "读取集中器地址";

                                            str_Comm = "68EE" + str_DeviceId + "50010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //集中器初始化
                                    //Type：60 | 60
                                    case "60":
                                        {
                                            str_xymc = "集中器初始化";
                                            str_xyjx = "集中器初始化";

                                            str_Comm = "68EE" + str_DeviceId + "60010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //设置集中器上报日结时间
                                    //Type：32 | 32
                                    //CommPam：⑴上传冻结时间（分）：⑵上传冻结时间（时）
                                    case "32":
                                        {
                                            str_xymc = "设置集中器上报日结时间";
                                            str_xyjx = "设置集中器上报日结时间";

                                            try
                                            {
                                                string[] str = CommPam.Split(':');

                                                //分
                                                string str_minute = str[0];
                                                str_minute = Convert.ToString(Convert.ToInt16(str_minute), 16);
                                                str_minute = "00" + str_minute;
                                                str_minute = str_minute.Substring(str_minute.Length - 2);

                                                //时
                                                string str_hour = str[1];
                                                str_hour = Convert.ToString(Convert.ToInt16(str_hour), 16);
                                                str_hour = "00" + str_hour;
                                                str_hour = str_hour.Substring(str_hour.Length - 2);

                                                str_Comm = "68EE" + str_DeviceId + "320300" + str_minute + str_hour + "01";
                                                str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                                str_Comm = str_Comm + "16";
                                                str_Comm = str_Comm.ToUpper();

                                                str_Result = Jzq_sql(DeviceId, Type);
                                            }
                                            catch
                                            {
                                                str_Result = "参数错误";
                                                break;
                                            }

                                            break;
                                        }

                                    //查询集中器上报日结时间
                                    //Type：33 | 33
                                    case "33":
                                        {
                                            str_xymc = "查询集中器上报日结时间";
                                            str_xyjx = "查询集中器上报日结时间";

                                            str_Comm = "68EE" + str_DeviceId + "33010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //查询集中器CCID 
                                    //Type：43 | 43
                                    case "43":
                                        {
                                            str_xymc = "查询集中器CCID";
                                            str_xyjx = "查询集中器CCID";

                                            str_Comm = "68EE" + str_DeviceId + "43010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }

                                    //进入bootloader功能
                                    //Type：A0 | A0
                                    case "A0":
                                        {
                                            str_xymc = "进入bootloader功能";
                                            str_xyjx = "进入bootloader功能";

                                            str_Comm = "68EE" + str_DeviceId + "A0010001";
                                            str_Comm = str_Comm + WebSdkCore.CS(str_Comm);
                                            str_Comm = str_Comm + "16";
                                            str_Comm = str_Comm.ToUpper();

                                            str_Result = Jzq_sql(DeviceId, Type);

                                            break;
                                        }
                                }
                                break;
                            }
                    }
                }
                catch
                {
                    str_Result = "设备编号不正确";
                }
            }
            return str_Result;
        }

        //2020-12-16
        //下载档案：多线程处理
        public static void jzq_type_100()
        {
            try
            {
                string sql = "select ybdz,xylx,jcdbh from rp_meter_bz where sbdz='" +
                                type_100_DeviceId + "' ORDER BY jcdbh";

                System.Data.DataTable Meter_Data = WebSdkCore.select(sql);

                if (Meter_Data.Rows.Count > 0)
                {
                    string str_jcdbh;
                    string str_blx;
                    string str_ybbh;

                    string str_lora = "";
                    string str_com = "";
                    string result = "";

                    int j = 0;
                    for (int i = 0; i < Meter_Data.Rows.Count; i++)
                    {
                        str_jcdbh = Meter_Data.Rows[i]["jcdbh"].ToString();
                        str_blx = Meter_Data.Rows[i]["xylx"].ToString();
                        str_ybbh = Meter_Data.Rows[i]["ybdz"].ToString();

                        if (str_lora == "")
                        {
                            str_lora += str_jcdbh + "." + str_blx + "." + str_ybbh;
                        }
                        else
                        {
                            str_lora += "," + str_jcdbh + "." + str_blx + "." + str_ybbh;
                        }
                        j = j + 1;

                        if (j == 20 || i == Meter_Data.Rows.Count - 1)
                        {
                            str_com = str_lora + ":4:50:17:0:1";

                            string lora_url = System.Configuration.ConfigurationManager.AppSettings["JZQ_URL"].ToString();

                            string url = lora_url + "?DeviceId=" + type_100_DeviceId + "&Type=10&CommPam=" + str_com;
                            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                            req.Method = "GET";
                            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                            Stream stream = resp.GetResponseStream();

                            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                            {
                                result = reader.ReadToEnd();
                            }

                            Thread.Sleep(3000);

                            str_lora = "";
                            j = 0;
                        }
                    }

                    if (result.IndexOf("成功") > 0)
                    {
                        str_Result = "成功";
                    }
                    else
                    {
                        str_Result = "失败";
                    }
                }
                else
                {
                    str_Result = "没有规划表计";
                }
            }
            catch
            {
                str_Result = "下载错误";
                return;
            }

            return;
        }

        //2020-12-18
        //清除下载档案：多线程处理
        public static void jzq_type_101()
        {
            try
            {
                string sql = "select ybdz,xylx,jcdbh from rp_meter_bz where sbdz='" +
                                type_100_DeviceId + "' ORDER BY jcdbh";

                System.Data.DataTable Meter_Data = WebSdkCore.select(sql);

                if (Meter_Data.Rows.Count > 0)
                {
                    string str_jcdbh;
                    string str_blx;
                    string str_ybbh;

                    string str_lora = "";
                    string str_com = "";
                    string result = "";

                    int j = 0;
                    for (int i = 0; i < Meter_Data.Rows.Count; i++)
                    {
                        str_jcdbh = Meter_Data.Rows[i]["jcdbh"].ToString();
                        str_blx = Meter_Data.Rows[i]["xylx"].ToString();
                        str_ybbh = Meter_Data.Rows[i]["ybdz"].ToString();

                        if (str_lora == "")
                        {
                            str_lora += str_jcdbh + "." + str_blx + "." + str_ybbh;
                        }
                        else
                        {
                            str_lora += "," + str_jcdbh + "." + str_blx + "." + str_ybbh;
                        }
                        j = j + 1;

                        if (j == 20 || i == Meter_Data.Rows.Count - 1)
                        {
                            str_com = str_lora + ":0:0:17:0:1";
                            
                            string lora_url = System.Configuration.ConfigurationManager.AppSettings["JZQ_URL"].ToString();

                            string url = lora_url + "?DeviceId=" + type_100_DeviceId + "&Type=10&CommPam=" + str_com;
                            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                            req.Method = "GET";
                            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                            Stream stream = resp.GetResponseStream();

                            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                            {
                                result = reader.ReadToEnd();
                            }

                            Thread.Sleep(3000);

                            str_lora = "";
                            j = 0;
                        }
                    }

                    if (result.IndexOf("成功") > 0)
                    {
                        str_Result = "成功";
                    }
                    else
                    {
                        str_Result = "失败";
                    }
                }
                else
                {
                    str_Result = "没有规划表计";
                }
            }
            catch
            {
                str_Result = "清除错误";
                return;
            }

            return;
        }

        //2021-03-26
        //清除下载档案：多线程处理（处理集中器有、平台没有）
        public static void jzq_type_102()
        {
            try
            {
                int ks_jcd = Convert.ToInt16(jcdbh_q);
                int js_jcd = Convert.ToInt16(jcdbh_z);

                if (js_jcd > ks_jcd)
                {
                    string str_jcdbh;
                    string str_blx;
                    string str_ybbh;

                    string str_lora = "";
                    string str_com = "";
                    string result = "";

                    int j = 0;
                    for (int i = ks_jcd; i <= js_jcd; i++)
                    {
                        str_jcdbh = i.ToString();
                        str_blx = "51";
                        str_ybbh = "12345678901234";

                        if (str_lora == "")
                        {
                            str_lora += str_jcdbh + "." + str_blx + "." + str_ybbh;
                        }
                        else
                        {
                            str_lora += "," + str_jcdbh + "." + str_blx + "." + str_ybbh;
                        }
                        j = j + 1;

                        if (j == 20 || i == js_jcd)
                        {
                            str_com = str_lora + ":0:0:17:0:1";

                            string lora_url = System.Configuration.ConfigurationManager.AppSettings["JZQ_URL"].ToString();

                            string url = lora_url + "?DeviceId=" + type_100_DeviceId + "&Type=10&CommPam=" + str_com;
                            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                            req.Method = "GET";
                            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                            Stream stream = resp.GetResponseStream();

                            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                            {
                                result = reader.ReadToEnd();
                            }

                            Thread.Sleep(3000);

                            str_lora = "";
                            j = 0;
                        }
                    }

                    if (result.IndexOf("成功") > 0)
                    {
                        str_Result = "成功";
                    }
                    else
                    {
                        str_Result = "失败";
                    }
                }
                else
                {
                    str_Result = "参数错误";
                }
            }
            catch
            {
                str_Result = "清除错误";
                return;
            }

            return;
        }

        public static string Jzq_sql(string DeviceId, string Type)
        {
            DateTime str_dt = DateTime.Now;

            bool operatorlog = WebSdkCore.insert_update_delete("insert into rp_meter_rz(sbdz,xynr,xyjx,xtsj,rzlx,xymc,czy,ybdz) values ('" +
                                                                DeviceId + "','" +
                                                                str_Comm + "','" +
                                                                str_xyjx + "','" +
                                                                str_dt + "','" +
                                                                str_rzlx + "','" +
                                                                str_xymc + "','" +
                                                                str_oper + "','')");
            if (operatorlog == true)
            {
                Global.Send_Jzq(str_Comm);
                str_Result = "成功";
            }
            else
            {
                str_Result = "写日志失败";
            }

            return str_Result;
        }
    }
}